package com.su.mw.mscthesis.login;

import java.util.List;

import org.jbpm.api.IdentityService;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.identity.Group;
import org.jbpm.api.identity.User;

import com.su.mw.mscthesis.util.MScThesisUtils;

/**
 * This class is used for various login purposes - checks, etc.
 * 
 */
public class LoginVerifier {

	/**
	 * Checks if the a user with the given userID exists and 
	 * is a member of the desired groupID. 
	 * 
	 * @param userID - the submitted userID
	 * @param desiredGroupID - the desired groupID, e.g. "students"
	 * @return
	 */
	public static boolean checkLogin(String userID, String desiredGroupID)
	{
		ProcessEngine engine = MScThesisUtils.PROCESS_ENGINE;
		IdentityService identityService = engine.getIdentityService();
		User user = identityService.findUserById(userID);
		if (user == null)
			return false;
		
		List<Group> userGroups = identityService.findGroupsByUser(userID);
		if (userGroups.isEmpty())
			return false;
		
		for (Group userGroup : userGroups)
			if (userGroup.getId().equalsIgnoreCase(desiredGroupID))
				return true;
		
		return false;
	}
	
}
